package com.springboot.template.config;

import com.springboot.template.timer.QuartzJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QuartzConfig {

    @Bean
    public JobDetail jobDetail() {
        return JobBuilder.newJob(QuartzJob.class) // 绑定要运行的任务类的类对象
                .withDescription("job") // 设置job的名称
                .storeDurably()
                // 设置storeDurably之后,当没有触发器指向这个JobDetail时,JobDetail也不会从
                // Spring容器中删除,如果不设置这行,就会自动从Spring容器中删除
                .build();
    }

    @Bean
    public Trigger trigger() {
        CronScheduleBuilder cronScheduleBuilder =
                CronScheduleBuilder.cronSchedule("0/1 * * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail())                    // 绑定JobDetail对象
                .withIdentity("trigger")          // 定义触发器名称
                .withSchedule(cronScheduleBuilder)      // 绑定Cron表达式
                .build();
    }
}
